﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel;
using System.Drawing.Drawing2D;

namespace TR0217.ControlEx
{
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(CollapsiblePanelSplitter), "CollapsiblePanelSplitter.bmp")]
    public class CollapsiblePanelSplitter : Splitter
    {
        CollapsiblePanel _collapsiblePanel = null;
        public CollapsiblePanel CollapsiblePanel
        {
            get { return _collapsiblePanel; }
            set { _collapsiblePanel = value; }
        }

        private Color _beginColor = Color.FromArgb(225, 238, 255);
        private Color _endColor = Color.FromArgb(83, 128, 209);
        private LinearGradientMode _gradientMode = LinearGradientMode.Horizontal;
        public Color BeginColor
        {
            get { return _beginColor; }
            set
            {
                _beginColor = value;
                this.Invalidate();
            }
        }

        public Color EndColor
        {
            get { return _endColor; }
            set
            {
                _endColor = value;
                this.Invalidate();
            }
        }

        public LinearGradientMode GradientMode
        {
            get { return _gradientMode; }
            set
            {
                _gradientMode = value;
                this.Invalidate();
            }
        }

        public CollapsiblePanelSplitter()
        {
            SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw, true);
            this.UpdateStyles();
        }

        //if (Parent != null)
        //{
        //    Bitmap bmp = new Bitmap(this.Width, this.Height, g);
        //    Parent.DrawToBitmap(bmp, this.Bounds);
        //    g.DrawImageUnscaled(bmp,0,0);
        //    bmp.Dispose();
        //}

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            Brush brush = new LinearGradientBrush(rect,
                _beginColor, _endColor, _gradientMode);
            pevent.Graphics.FillRectangle(brush, rect);
            brush.Dispose();
        }


        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (_collapsiblePanel != null && _collapsiblePanel.Collapsed)
                return;
            base.OnMouseDown(e);
        }

    }
}
